//
//  QZLoginVC.m
//  map
//
//  Created by 000 on 16/9/12.
//  Copyright © 2016年 faner. All rights reserved.
//

#import "QZLoginVC.h"
#import "QZRegisterVC.h"
#import <BmobSDK/Bmob.h>
#import "DetailModel.h"
#import "MBProgressHUD+MS.h"
//#import "MapVC.h"
#import "QZManagerVC.h"
#import "constant.h"
#import "QZNetworkTool.h"
#import "QZViewController.h"
@interface QZLoginVC ()
{
    BOOL logSuccess;
    BOOL manager;

}
- (IBAction)loadButtonClick:(id)sender;
- (IBAction)registerBtnClick:(id)sender;

@end

@implementation QZLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    logSuccess =NO;
    [self.userTextFiled becomeFirstResponder];
    [self addTitle];
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tapGest];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)tap:(UIGestureRecognizer *)tapGesture
{
    [self.userTextFiled resignFirstResponder];
    [self.passwordTextFiled resignFirstResponder];
}

//添加自定义标题
-(void)addTitle
{
    self.navigationController.navigationBarHidden = YES;
    self.navigationItem.title = @"登陆";
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.hidesBackButton = YES;//隐藏返回按钮
}
- (IBAction)loadButtonClick:(UIButton *)sender {
    sender.enabled = NO;
    acc = self.userTextFiled.text;
    psw = self.passwordTextFiled.text;
    if ([QZNetworkTool isEnableInternet]||[QZNetworkTool isEnableWifi]) {
//        NSLog(@"wifi网路");
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        
        [BmobUser loginInbackgroundWithAccount:acc andPassword:psw block:^(BmobUser *user, NSError *error) {
            if (user) {
                [DetailModel shareManger].loginACC = nil;
                [userDefaults setObject:acc forKey:@"detailSave"];
                [DetailModel shareManger].loginACC = acc;
                [userDefaults synchronize];
#pragma mark 保存登录状态
                logSuccess = YES;
                [userDefaults setBool:logSuccess forKey:@"logState"];
                [userDefaults synchronize];
                if ([acc isEqualToString:@"18210637213"]) {
//                    mapVC.judgeType = QZAccountType;
                    QZManagerVC *managerVC = [[QZManagerVC alloc] init];
                    [self.navigationController pushViewController:managerVC animated:YES];
                    manager= YES;
                    [userDefaults setBool:manager forKey:@"manager"];
                } else {
                    QZViewController *userVC = [[QZViewController alloc] init];
                    [self.navigationController pushViewController:userVC animated:YES];
                }
            } else {
                [MBProgressHUD showError:@"帐号或密码错误"];
            }
        }];

    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前没可用的网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    sender.enabled = YES;
}


- (IBAction)registerBtnClick:(id)sender {
    QZRegisterVC *registerVC = [[QZRegisterVC alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
    
}
@end
